<?php
declare (strict_types=1);

namespace app\mall\logic\order\admin;

use app\common\model\payment\admin\PaymentOrderItem as OrderModel;
use think\Paginator;

/**
 * 后台商品订单逻辑层
 */
class Order extends OrderModel
{

    public function getList(array $map): Paginator
    {
        return $this->withJoin(['product', 'main', 'transaction'])->with(['transaction', 'uname'])
            ->where('product.mhm_id', request()->mhm_id)->where('product_type_id', 11)->whereIn('main.status', [10, 20, 30, 40])
            ->where($map)->order('order.create_time', 'DESC')->paginate(static::getListRows())
            ->bindAttr('transaction', ['payment_type_text', 'payment_time', 'out_trade_no'])->hidden(['main', 'transaction'])
            ->each(function ($i) {
                $i->consignee    = $i->addrinfo['consignee'] ?? '';
                $i->phone        = $i->addrinfo['phone'] ?? '';
                $i->address      = ($i->addrinfo['areatext'] ?? '') . ' ' . ($i->addrinfo['address'] ?? '');
                $i->num          = $i->ext_data['num'] ?? 0;
                $i->credit_use   = $i->credit['credit'] ?? 0;
                $i->payment_time = strval($i->payment_time);
            });
    }

    public function getDetail($order_no)
    {
        return $this->with(['product', 'main', 'uname'])->where('order_no', $order_no)->findOrEmpty();
    }

    public function editExpress($order_sub_no, $param)
    {
        // 数据验证
        $this->getValidate()->scene('express')->failException()->check($param);
        $order            = $this->where('order_sub_no', $order_sub_no)->findOrEmpty();
        $extData          = $order->ext_data;
        $extData['title'] = $param['title'];
        $extData['order'] = $param['order'];
        $order->ext_data  = $extData;
        $order->save();
    }
}
